package cn.work.control;

import cn.work.bean.Doctor;
import cn.work.service.DoctorService;
import com.google.gson.Gson;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/doctor/*")
public class DoctorServlet extends HttpServlet {
    private DoctorService doctorService;
    private Gson gson;

    @Override
    public void init() throws ServletException {
        doctorService = new DoctorService();
        gson = new Gson();
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("application/json");
        response.setCharacterEncoding("UTF-8");

        String pathInfo = request.getPathInfo();
        if (pathInfo == null || pathInfo.equals("/")) {
            // 获取所有医师列表
            List<Doctor> doctors = doctorService.getAllDoctors();
            response.getWriter().write(gson.toJson(doctors));
        } else if (pathInfo.startsWith("/search")) {
            // 搜索医师
            String keyword = request.getParameter("keyword");
            List<Doctor> doctors = doctorService.searchDoctors(keyword);
            response.getWriter().write(gson.toJson(doctors));
        } else {
            // 获取单个医师信息
            try {
                int id = Integer.parseInt(pathInfo.substring(1));
                Doctor doctor = doctorService.getDoctorById(id);
                if (doctor != null) {
                    response.getWriter().write(gson.toJson(doctor));
                } else {
                    response.setStatus(HttpServletResponse.SC_NOT_FOUND);
                }
            } catch (NumberFormatException e) {
                response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
            }
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("application/json");
        response.setCharacterEncoding("UTF-8");

        String pathInfo = request.getPathInfo();
        if ("/add".equals(pathInfo)) {
            // 添加医师
            Doctor doctor = gson.fromJson(request.getReader(), Doctor.class);
            boolean success = doctorService.addDoctor(doctor);
            sendResponse(response, success);
        } else if ("/update".equals(pathInfo)) {
            // 更新医师信息
            Doctor doctor = gson.fromJson(request.getReader(), Doctor.class);
            boolean success = doctorService.updateDoctor(doctor);
            sendResponse(response, success);
        } else if ("/delete".equals(pathInfo)) {
            // 删除医师
            int id = Integer.parseInt(request.getParameter("id"));
            boolean success = doctorService.deleteDoctor(id);
            sendResponse(response, success);
        } else {
            response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
        }
    }

    private void sendResponse(HttpServletResponse response, boolean success) throws IOException {
        if (success) {
            response.getWriter().write("{\"success\":true}");
        } else {
            response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            response.getWriter().write("{\"success\":false}");
        }
    }
}